package com.currency.mall.ui.my.recharge;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.entity.RechargeBean;
import com.currency.mall.entity.RechargeStatus;
import com.currency.mall.event.OrderEvent;
import com.currency.mall.netWork.NetWork;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbase.util.Tools;
import com.mifengs.commonbusines.entity.ResponseBean;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/1/2.
 */

public class WaitConfirmActivity extends BaseActivity {
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.iv_order_num)
    ImageView ivOrderNum;
    @BindView(R.id.alipay_label)
    TextView alipayLabel;
    @BindView(R.id.iv_hint)
    ImageView ivHint;
    @BindView(R.id.tv_alipay_label)
    TextView tvAlipayLabel;
    @BindView(R.id.tv_cancle_order)
    TextView tvCancleOrder;
    @BindView(R.id.tv_recharge_price)
    TextView tvRechargePrice;
    @BindView(R.id.iv_copy_label)
    ImageView ivCopyLabel;
    @BindView(R.id.rl_cancle_order)
    RelativeLayout rlCancleOrder;
    @BindView(R.id.ll_explain)
    LinearLayout llExplain;
    @BindView(R.id.tv_order_statu)
    TextView tvOrderStatu;
    @BindView(R.id.tv_order_statu_explain)
    TextView tvOrderStatuExplain;
    private RechargeStatus rechargeStatus;
    private RechargeBean.RechargeInfo rechargeInfo;
    private String rechargePrice;
    private Dialog rechargeDilaog;
    private int status = 0;
    private String payType = "BANK";
    private int type;
    private Dialog hintDialogShow;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_wait_confirm);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        rechargeInfo = (RechargeBean.RechargeInfo) getIntent().getSerializableExtra("rechargeInfo");
        rechargePrice = getIntent().getStringExtra("price");
        status = getIntent().getIntExtra("status", 0);
        rechargeStatus = (RechargeStatus) getIntent().getSerializableExtra("rechargeStatus");
        type = getIntent().getIntExtra("type", 0);
        init();
    }

    private void init() {
        if (type == 1) {
            rlCancleOrder.setVisibility(View.GONE);
            llExplain.setVisibility(View.GONE);
            tvOrderStatu.setText("充值成功");
            tvOrderStatuExplain.setText("订单已完成");
        } else if (type == 2) {
            rlCancleOrder.setVisibility(View.GONE);
            llExplain.setVisibility(View.GONE);
            tvOrderStatu.setText("已取消");
            tvOrderStatuExplain.setText("订单已取消，无法查看支付信息");
        }
        if(rechargeStatus != null){
            tvRechargePrice.setText("¥ " + rechargeStatus.getRecharSum());
            tvOrderNum.setText(rechargeStatus.getOrderNum());
            tvAlipayLabel.setText(rechargeStatus.getRecharRemark());
            tvOrderTime.setText(rechargeStatus.getOrderTime());
            if ("ALIPAY".equals(rechargeStatus.getType())) {
                payType = "ALIPAY";
            } else {
                payType = "BANK";
            }
        }else{
            tvRechargePrice.setText("¥ " + rechargePrice);
            if(status == 0){
                payType = "BANK";
            }else{
                payType = "ALIPAY";
            }
            tvOrderNum.setText(rechargeInfo.getOrderNum());
            tvAlipayLabel.setText(rechargeInfo.getRecharRemark());
            tvOrderTime.setText(rechargeInfo.getOrderTime());
        }

    }

    private void rechargeOrderDilaogShow() {
        rechargeDilaog = new Dialog(getContext(), R.style.dialog);
        rechargeDilaog.setContentView(R.layout.dialog_not_title);
        rechargeDilaog.show();
        rechargeDilaog.setCancelable(false);
        TextView tvInfo = (TextView) rechargeDilaog.findViewById(R.id.tv_info);
        tvInfo.setText("每个用户每天只能取消10笔订单，请谨慎操作");
        Button okBtn = rechargeDilaog.findViewById(R.id.bt_ok);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sumbitRechargeState();
                rechargeDilaog.dismiss();

            }
        });
        Button cancleBtn = rechargeDilaog.findViewById(R.id.bt_cancle);
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rechargeDilaog.dismiss();
            }
        });
    }

    public void sumbitRechargeState() {
        HashMap<String, Object> hashMap = new HashMap<>();
        if(rechargeStatus != null){
            hashMap.put("id", rechargeStatus.getId());
        }else{
            hashMap.put("id", rechargeInfo.getId());
        }
        hashMap.put("payType", payType);
        hashMap.put("payState", "PAY_CANCEL");
        NetWork.getUserApi().rechargeState(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("充值订单已取消", 200);
                            rlCancleOrder.setVisibility(View.GONE);
                            llExplain.setVisibility(View.GONE);
                            tvOrderStatu.setText("已取消");
                            tvOrderStatuExplain.setText("订单已取消，无法查看支付信息");
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }

    @OnClick({R.id.ll_back, R.id.tv_cancle_order, R.id.iv_order_num, R.id.iv_copy_label,R.id.iv_hint})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_cancle_order:
                rechargeOrderDilaogShow();
                break;
            case R.id.iv_order_num:
                copyValue("订单号： ", rechargeStatus.getOrderNum());
                break;
            case R.id.iv_copy_label:
                copyValue("参考号： ", rechargeStatus.getOrderNum());
                break;
            case R.id.iv_hint:
                hintDialogShow();
                break;
        }
    }

    private void hintDialogShow() {
        hintDialogShow = new Dialog(getContext(), R.style.dialog);
        hintDialogShow.setContentView(R.layout.dialog_not_title);
        hintDialogShow.show();
        hintDialogShow.setCancelable(false);
        View view = (View)hintDialogShow.findViewById(R.id.v_view);
        TextView tvInfo = (TextView) hintDialogShow.findViewById(R.id.tv_info);
        tvInfo.setText("重要提醒：请在付款信息中备注付款参考号");
        Button okBtn = hintDialogShow.findViewById(R.id.bt_ok);
        view.setVisibility(View.GONE);
        okBtn.setVisibility(View.GONE);
        Button cancleBtn = hintDialogShow.findViewById(R.id.bt_cancle);
        cancleBtn.setText("知道了");
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hintDialogShow.dismiss();
            }
        });
    }


    private void copyValue(String name, String value) {
        Tools.copy(value, getContext());
        ToastUitl.show(name + value + "    复制成功", 200);
    }
}
